<?php

include_once 'data_layer.php';
include_once 'tasks_logic.php';

//functions
// ===============================
function checkBrowser() {
    $chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
    if (!$chrome) {
        die("<script>alert('You must use Google Chrome.')</script>");
    }
}

// ===============================
function getClientIP() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if ($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if ($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if ($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

// ===============================
function calculateIMI($postData) {
    $im = 0;
    $count = 0;
    for ($i = 1; $i <= sizeof($postData); $i++) {
        if (isset($postData['inputQ' . $i])) {
            $answer = intval($postData['inputQ' . $i]);
            $answerReverse = 8 - $answer;

            if ($i == 3 || $i == 4) {
                $im+=$answerReverse;
            } else {
                $im+=$answer;
            }

            $count++;
        }
    }

    return floatval(number_format($im / $count, 2, '.', ''));
}

// ===============================
function calculatePT($postData) {
    return $postData;
    $extraversion = array();
    $agreeableness = array();
    $conscientiousness = array();
    $neuroticism = array();
    $openness = array();

    
    for ($i = 1; $i <= sizeof($postData); $i++) {
        if (isset($postData['inputQ' . $i])) {
            $answer = intval($postData['inputQ' . $i]);
            $answerReverse = 6 - $answer;

            //==================
            //extraversion (direct)
            if ($i == 1 || $i == 11 || $i == 16 || $i == 26 || $i == 36) {
                array_push($extraversion, $answer);
            }
            //extraversion (reverse)
            else if ($i == 6 || $i == 21 || $i == 31) {
                array_push($extraversion, $answerReverse);
            }
            //==================
            //agreeableness (direct)
            else if ($i == 7 || $i == 17 || $i == 22 || $i == 32 || $i == 42) {
                array_push($agreeableness, $answer);
            }
            //agreeableness (reverse)
            else if ($i == 2 || $i == 12 || $i == 27 || $i == 37) {
                array_push($agreeableness, $answerReverse);
            }
            //==================
            //conscientiousness (direct)
            else if ($i == 3 || $i == 13 || $i == 28 || $i == 33 || $i == 38) {
                array_push($conscientiousness, $answer);
            }
            //conscientiousness (reverse)
            else if ($i == 8 || $i == 18 || $i == 23 || $i == 43) {
                array_push($conscientiousness, $answerReverse);
            }
            //==================
            //neuroticism (direct)
            else if ($i == 4 || $i == 14 || $i == 19 || $i == 29 || $i == 39) {
                array_push($neuroticism, $answer);
            }
            //neuroticism (reverse)
            else if ($i == 9 || $i == 24 || $i == 34) {
                array_push($neuroticism, $answerReverse);
            }
            //==================
            //openness (direct)
            else if ($i == 5 || $i == 10 || $i == 15 || $i == 20 || $i == 25 || $i == 30 || $i == 40 || $i == 44) {
                array_push($openness, $answer);
            }
            //openness (reverse)
            else if ($i == 35 || $i == 41) {
                array_push($openness, $answerReverse);
            }
        }
    }

    //calculate the traits
    $results = array(
        "extraversion" => array_sum($extraversion),
        "agreeableness" => array_sum($agreeableness),
        "conscientiousness" => array_sum($conscientiousness),
        "neuroticism" => array_sum($neuroticism),
        "openness" => array_sum($openness),
    );

    return $results;
}

// ===============================
function getRecipes() {
    $filename = 'recipes';
    return file($filename, FILE_IGNORE_NEW_LINES);
}

// ===============================
function createParticipantID() {
    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $pid = (isset($_SESSION['pid_prefix']) ? $_SESSION['pid_prefix'] : '') . "P" . mt_rand(100, 9999);
    while (participantExists($pid)) {
        $pid = "P" . mt_rand(100, 9999);
    }

    return $pid;
}

// ===============================
function saveRecipeRate($recipeNumber, $recipeRate) {
    //create a new document
    if (getRecipeRate($recipeNumber) == 0) {
        saveNewRecipeRate($recipeNumber, $recipeRate);
    }
    //update
    else {
        updateRecipeRate($recipeNumber, $recipeRate);
    }
}

// ===============================
function setRecipeNumber($recipeNumber) {
    if (!isset($_SESSION)) {
        session_start();
    }

    $_SESSION['recipe_number'] = $recipeNumber;
}

//post function dispatcher
// ===============================
if (isset($_POST["function_name"])) {
    $function_name = $_POST["function_name"];

    //start the session
    if (!isset($_SESSION)) {
        session_start();
    }

    //setRecipeNumber()
    if ($function_name == "setRecipeNumber") {
        setRecipeNumber($_POST["recipe_number"]);
    }
    //getRecipeRate()
    else if ($function_name == "getRecipeRate") {
        echo getRecipeRate($_POST["recipe_number"]);
    }
    //saveRecipeRate()
    else if ($function_name == "saveRecipeRate") {
        saveRecipeRate($_POST["recipe_number"], $_POST["recipe_rate"]);
    }
}

